package cn.moukou.dictator.service.relation;

import cn.moukou.dictator.dao.node.KVNodeDAO;
import cn.moukou.dictator.dao.relation.*;
import cn.moukou.dictator.pojo.node.KVNode;
import cn.moukou.dictator.pojo.relation.*;
import cn.moukou.dictator.util.VOUtil;
import cn.moukou.dictator.vo.RelationVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RelationService {
//    @Autowired
//    STUDENT_ACTIVITY_DAO st_ac_dao;
//    @Autowired
//    STUDENT_AWARD_DAO st_aw_dao;
//    @Autowired
//    STUDENT_CLASS_DAO st_cl_dao;
//    @Autowired
//    STUDENT_COMPETITION_DAO st_com_dao;
//    @Autowired
//    STUDENT_ORG_DAO st_org_dao;
//    @Autowired
//    STUDENT_TEACHER_DAO st_te_dao;
    @Autowired
    KVNODE_RELATION_DAO nodeRelationDAO;
    @Autowired
    KVNodeDAO nodeDAO;


    public Map<String, Object> getAllRelationInMap() {
        Map<String, Object> res = new HashMap<>();
        List<KVNODE_RELATION> st_ac = nodeRelationDAO.findByType("student_activity");
        List<KVNODE_RELATION> st_aw = nodeRelationDAO.findByType("student_award");
        List<KVNODE_RELATION> st_cl = nodeRelationDAO.findByType("student_class");
        List<KVNODE_RELATION> st_com = nodeRelationDAO.findByType("student_competition");
        List<KVNODE_RELATION> st_org = nodeRelationDAO.findByType("student_organization");
        List<KVNODE_RELATION> st_te = nodeRelationDAO.findByType("student_teacher");

        res.put("st_ac", VOUtil.getRelationVOList(st_ac));
        res.put("st_aw", VOUtil.getRelationVOList(st_aw));
        res.put("st_cl", VOUtil.getRelationVOList(st_cl));
        res.put("st_com", VOUtil.getRelationVOList(st_com));
        res.put("st_org", VOUtil.getRelationVOList(st_org));
        res.put("st_te", VOUtil.getRelationVOList(st_te));

        return res;
    }

    public Map<String, Object> getAllRelationInPage(Integer page, Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
        List<RelationVO> voList = new ArrayList<>();
        voList = VOUtil.getRelationVOList(nodeRelationDAO.customFindAllOrderByType());
        return sliceListByPage(voList, page, pageSize);
    }

    public Map<String, Object> getRelationByType(String type, int page, int pageSize) {
        List<RelationVO> voList = new ArrayList<>();
        voList =  VOUtil.getRelationVOList(nodeRelationDAO.findByType(type));
        return sliceListByPage(voList, page, pageSize);
    }

    public RelationVO addRelation(Long start, Long end, String type, Map<String, Object> params) {
        KVNODE_RELATION relation = new KVNODE_RELATION();
        relation.setRelationType(type);
        relation.setRelationLabel((String) params.get("label"));
        KVNode startNode = nodeDAO.findById(start).orElse(null);
        KVNode endNode = nodeDAO.findById(end).orElse(null);
        if(startNode == null || endNode == null) {
            return null;
        }
        relation.setStartNode(startNode);
        relation.setEndNode(endNode);
        return new RelationVO(nodeRelationDAO.save(relation));
    }

    public void deleteRelation(Long id)
    {
        nodeRelationDAO.deleteById(id);
    }

    private Map<String, Object> sliceListByPage(List<RelationVO> voList, Integer page, Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
        int total = voList.size();
        int lo = (page - 1) * pageSize;
        int hi = page * pageSize;
        if(hi > total) {
            hi = total;
        }
        if(hi - lo > 0) {
            map.put("relationList", voList.subList(lo, hi));
        } else {
            map.put("relationList", new ArrayList<>());
        }
        map.put("total", total);
        return map;
    }

    public Map<String, Object> getOneRelation(Integer id) {
        Map<String, Object> res = new HashMap<>();
        KVNODE_RELATION relation = nodeRelationDAO.findById(new Long(id)).orElse(null);
        if(relation!=null) {
            RelationVO relationVO = new RelationVO(relation);
            res.put("relation", relation);
        } else {
            res.put("relation", null);
        }
        return res;
    }
}
